﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using xna = Microsoft.Xna.Framework;
using URWPGSim2D.Common;
using URWPGSim2D.StrategyLoader;
using URWPGSim2D.FishDecision;

namespace URWPGSim2D.Strategy
{
    public class Strategy : MarshalByRefObject, IStrategy
    {
        #region reserved code never be changed or removed
        /// <summary>
        /// override the InitializeLifetimeService to return null instead of a valid ILease implementation
        /// to ensure this type of remote object never dies
        /// </summary>
        /// <returns>null</returns>
        public override object InitializeLifetimeService()
        {
            //return base.InitializeLifetimeService();
            return null; // makes the object live indefinitely
        }
        #endregion

        /// <summary>
        /// 决策类当前对象对应的仿真使命参与队伍的决策数组引用 第一次调用GetDecision时分配空间
        /// </summary>
        private Decision[] decisions = null;

        /// <summary>
        /// 获取队伍名称 在此处设置参赛队伍的名称
        /// </summary>
        /// <returns>队伍名称字符串</returns>
        public string GetTeamName()
        {
            return "Venus Test Team";
        }


        public int Flag = 0;
        public int Flash = 0;
        public int dubble = 0;


        /// <summary>
        /// 获取当前仿真使命（比赛项目）当前队伍所有仿真机器鱼的决策数据构成的数组
        /// </summary>
        /// <param name="mission">服务端当前运行着的仿真使命Mission对象</param>
        /// <param name="teamId">当前队伍在服务端运行着的仿真使命中所处的编号 
        /// 用于作为索引访问Mission对象的TeamsRef队伍列表中代表当前队伍的元素</param>
        /// <returns>当前队伍所有仿真机器鱼的决策数据构成的Decision数组对象</returns>
        public Decision[] GetDecision(Mission mission, int teamId)
        {
            // 决策类当前对象第一次调用GetDecision时Decision数组引用为null
            if (decisions == null)
            {// 根据决策类当前对象对应的仿真使命参与队伍仿真机器鱼的数量分配决策数组空间
                decisions = new Decision[mission.CommonPara.FishCntPerTeam];
            }

            //判断球进洞设置标签
            int b0 = Convert.ToInt32(mission.HtMissionVariables["Ball0InHole"]);
            int b1 = Convert.ToInt32(mission.HtMissionVariables["Ball1InHole"]);
            int b2 = Convert.ToInt32(mission.HtMissionVariables["Ball2InHole"]);
            int b3 = Convert.ToInt32(mission.HtMissionVariables["Ball3InHole"]);
            int b4 = Convert.ToInt32(mission.HtMissionVariables["Ball4InHole"]);
            int b5 = Convert.ToInt32(mission.HtMissionVariables["Ball5InHole"]);
            
            //一鱼区域判断
            int yi = FishDecision.FishDecision.Yi(mission.TeamsRef[0].Fishes[1].PositionMm);
            int er = FishDecision.FishDecision.Er(mission.TeamsRef[0].Fishes[1].PositionMm);
            int san = FishDecision.FishDecision.San(mission.TeamsRef[0].Fishes[1].PositionMm);
            int fuyi = FishDecision.FishDecision.Fuyi(mission.TeamsRef[0].Fishes[1].PositionMm);
            int ling = FishDecision.FishDecision.Ling(mission.TeamsRef[0].Fishes[1].PositionMm);

            //二鱼区域判断
            int one = FishDecision.FishDecision.One(mission.TeamsRef[0].Fishes[0].PositionMm);
            int two = FishDecision.FishDecision.Two(mission.TeamsRef[0].Fishes[0].PositionMm);
            int three= FishDecision.FishDecision.Three(mission.TeamsRef[0].Fishes[0].PositionMm);
            int four = FishDecision.FishDecision.Four(mission.TeamsRef[0].Fishes[1].PositionMm);

           

            //1鱼游动
         

            if (Flag==0)
            { 

                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[1], 0, 1, new xna.Vector3(1300, 0, -600),90);

                if (yi==1)
                {
                    Flag =1;
                }

            }
            //1鱼顶三球
            if(Flag==1)
            {

                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[1], 0, 1, 3, new xna.Vector3(-480, 0, -340), 5,5,5,6,3,1);//-320

                if (b3==1)
                {
                    Flag = 2;
                }

            }
            //一鱼返回
            if(Flag==2)
            {

                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[1], 0, 1, new xna.Vector3(984, 0, -316), 90);
                if (er==1)
                {
                    Flag = 3;
                }

            }
            //一鱼顶一球
            if(Flag==3)
            {
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[1], 0, 1, 1, new xna.Vector3(-230, 0, -190), 5, 5, 5, 6, 12 ,1);//-160
                if(b1==1)
                {
                    Flag = 4;
                }
            }
            //一鱼返回
            if(Flag==4)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[1], 0, 1, new xna.Vector3(1200, 0, 120), 90);
                if(san==1)
                {
                    Flag = 5;
                }
            }
            //一鱼顶二球
            if(Flag==5)
            {
                 FishDecision.FishDecision.Dribble_Will(mission, ref decisions[1], 0, 1, 2, new xna.Vector3(-250, 0, 150), 5, 5, 5, 6, 12, 1);//160
                if(b2==1)
                {
                    Flag = 6;
                }
            }
            //一鱼罚站
            if (Flag == 6)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[1], 0, 1, new xna.Vector3(-900, 0, -700), 90);
            }
           
          
            
           





            //0鱼游动
            if(Flash==0)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(1200, 0, 600), 0);
                if (one==1)
                {
                 Flash = 1;
                 }
            }
            
            //0鱼顶4球
            if(Flash==1)
            {
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0], 0, 0, 4, new xna.Vector3(-480, 0,-50), 5,5,5,6,3,1);//0
                if(b0==1)
                {
                    Flash = -5;
                }
                if(b4==1)
                {
                    Flash = 3;
                }
            }
            //0鱼顶四球顺带零球
            if(Flash==-5)
            {
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0], 0, 0, 4, new xna.Vector3(-480, 0,-10), 5, 5, 5, 6, 3, 1);//0
                if(b4==1)
                {
                    Flash = 3;
                }
            }

            //0鱼没有顺带顶进零球的弥补
            if(b0==0&&Flash==3)
            {
                dubble = 100;
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0], 0,0, 0, new xna.Vector3(0, 0, -20), 5, 5, 5, 6, 3, 1);//0
            }
            //0鱼已经顶进4和0球
            if(b0==1&&Flash==3)
            {
                Flash = 4;
            }
            //0鱼返回
            if(Flash==4)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(1160, 0, 530), -45);
                if(three==1)
                {
                    Flash = 5;
                }
            }
            //零鱼顶五球
            if(Flash==5)
            {
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0], 0, 0, 5, new xna.Vector3(-460, 0, 297), 5, 5, 5, 6, 12, 1);//320
                if(b5==1)
                {
                    Flash = 6;
                }
            }
            //零鱼罚站
            if(Flash==6)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-1400, 0, 900), -45);
            }
           
           


            






























            return decisions;
        }
    }
}
